layui.use(['tableEdit', 'table', 'form', 'laydate',"selectInput","excel"], function () {
    let layer = layui.layer //弹层
        ,table = layui.table //表格
        ,form = layui.form,
        $ = layui.jquery,
        laydate = layui.laydate,
        excel = layui.excel,
        selectInput = layui.selectInput;
    lay('.starttime').each(function(){
        laydate.render({
            elem: this
        });
    });


    function getAllJson(jsons) {
        let tmpitem=[]
        for (let item of jsons) {
            let tmparr={}
            tmparr["value"]=item["code"];
            tmparr["name"]=item["name"];
            if(item["users"]){
                for(let it of item["users"]){
                    item["list"].push(it)
                }
            }
            if(item["list"] && item["list"].length){
                tmparr["children"]=   getAllJson(item["list"]);
                tmpitem.push(tmparr)
            }else{
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }
    function getAllJsonSal(jsons) {
        let tmpitem=[]
        for (let item of jsons) {
            let tmparr={}
            tmparr["value"]=item["name"];
            tmparr["name"]=item["name"];
            if(item["users"]){
                for(let it of item["users"]){
                    item["list"].push(it)
                }
            }
            if(item["list"] && item["list"].length){
                tmparr["children"]=   getAllJsonSal(item["list"]);
                tmpitem.push(tmparr)
            }else{
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }

    function getxmselect(el,name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon:true,
                indent: 20,
                expandedKeys:false
            },
            height: 'auto',
            data: data,
            on: function ({ arr, change, isAdd }) {
                return this
            }
        })
    }

    let salesmanList=[];
    let customerTypeDataList = [];
    layer.ready(function () {
        //客户类型
        let customerTypeData = getdictionary(6, 27);
        let customerTypeList = [];
        for(let i in customerTypeData){
            let customerTypeTemp = {
                name: customerTypeData[i].name,
                value: customerTypeData[i].code
            }
            customerTypeList.push(customerTypeTemp);
        }
        customerTypeDataList = getxmselect("#customerType", "customerType", customerTypeList);



        //获取当前用户的部门及人员结构树
        let treeList = getCurrentDepartmentUser();

        if(treeList && treeList.list){
            let userTree = getAllJson(treeList.list);
            let sal=getAllJsonSal(treeList.list);
            salesmanList=getxmselect("#salesmanCode","salesmanCode", userTree);
        }else {
            salesmanList=getxmselect("#salesmanCode","salesmanCode", [{name: user.name, value: user.name}]);
        }


        setCustomerInfo("")
        function setCustomerInfo(initData) {
            //客户名称
            let ins = selectInput.render({
                elem: '#customerSelect',
                name: 'customerSelect',
                initValue: initData,
                hasSelectIcon: true,
                placeholder: '请输入名称',
                data: [],
            })
            //监听输入
            ins.on('itemInput(customerSelect)', function (obj) {
                let tempData = [];
                $.ajax({
                    url: "basicInformation/getCustomerInfo?info=" + obj.value,
                    async: false,
                    success: function (res) {
                        let customerInfo;
                        if (res.status == 200) {
                            customerInfo = res.data.customerInfo;
                            for (let i in customerInfo) {
                                customerInfo[i]["value"] = customerInfo[i].code;
                                tempData.push(customerInfo[i]);
                            }
                        }
                    }
                })
                ins.addSelect(tempData)
            })

            // 监听select 选择事件
            ins.on('itemSelect(customerSelect)', function (obj) {
                let code = obj.data;
                let name = obj.textContent;
                $("#customerCode").val(code);
                $("#customerName").val(name);
            });
        }




    });
    form.render();//表单刷新
    let initData = [];
    table.render({
        elem: '#collectionSelect'
        ,data:initData
        ,toolbar: '#collectionToolbar'
        ,cols: [[
            {field: 'numbers',width: 50,type:'numbers',title:'序号'}
            , {field: 'importance', title: '重要程度', sort: true, width: 120, align: 'center'}
            , {field: 'inquiryTime', title: '询盘日期', sort: true, width: 120, align: 'center'}
            , {field: 'oddNumber', title: '询价单号', sort: true, width: 120, align: 'center'}
            , {field: 'salesmanCName', title: '销售名英文名', sort: true, width: 120, align: 'center'}
            , {field: 'salesmanEName', title: '销售名中文名', sort: true, width: 120, align: 'center'}
            , {field: 'customerName', title: '客户姓名', sort: true, width: 120, align: 'center'}
            , {field:'email', title:'邮箱',sort:true,width: 120, align: 'center'}
            , {field: 'country', title: '国家', sort: true, width: 120, align: 'center'}
            , {field:'platformInquiryNo', title:'平台询价单号',sort:true,width: 120, align: 'center'}
            // , {field: 'img', title: '图片', sort: true, width: 120, align: 'center'}
            , {field:'productName', title:'产品名称',sort:true,width: 120, align: 'center'}
            , {field:'chineseDesc', title:'中文描述',sort:true,width: 120, align: 'center'}
            , {field: 'englishDesc', title: '英文描述', sort: true, width: 120, align: 'center'}
            , {field: 'customerType', title: '客户类型', sort: true, width: 120, align: 'center'}
            , {field: 'businessType', title: '业务类型', sort: true, width: 120, align: 'center'}
            , {field: 'sourceOfCustomers', title: '客户来源', sort: true, width: 120, align: 'center'}
            , {field: 'website', title: '公司网站', sort: true, width: 120, align: 'center'}
        ]]
        ,id: 'maininformationtableid'
        ,height: $(document).height() - $('#collectionSelect').offset().top - 75,
        //开启分页
        page: true,
        limits: [300,600,900],
    });


//搜索提交
    form.on('submit(submitform)', function(data){
        table.reload('maininformationtableid', {
            page: {
                limit:300,
                curr: 1
            },
            url:'inquiry/inquiryStatisticsListByPage',
            where:  data.field
        }, 'data');
        return false;
    });

    form.on('submit(collectionExcel)', function (data) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: data.field,
            url:'inquiry/inquiryStatisticsExport',
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 120,
                        'AJ': 120
                    }, 120);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '询盘统计.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });


    document.getElementById('clear').onclick = function(){
        salesmanList.setValue([ ]);
        customerTypeDataList.setValue([ ]);
        $("#customerCode").val('');
        $("#customerName").val('');
    };


});
